
package com.ukao.cashregister.pesenter;

import android.content.Context;
import android.util.Log;

import com.ukao.cashregister.base.BasePresenter;
import com.ukao.cashregister.bean.BalanceBean;
import com.ukao.cashregister.bean.Coupons;
import com.ukao.cashregister.bean.PayInfoBean;
import com.ukao.cashregister.bean.ReceiptBean;
import com.ukao.cashregister.bean.StringBean;
import com.ukao.cashregister.consts.Constant;
import com.ukao.cashregister.consts.SaveParamets;
import com.ukao.cashregister.eventbus.PayEvent;
import com.ukao.cashregister.retrofit.ApiCallback;
import com.ukao.cashregister.utils.CheckUtils;
import com.ukao.cashregister.utils.DecimalUtil;
import com.ukao.cashregister.utils.L;
import com.ukao.cashregister.utils.T;
import com.ukao.cashregister.utils.Utils;
import com.ukao.cashregister.view.GatheringView;

import java.util.HashMap;

import static com.ukao.cashregister.R.id.balance;

/**
 * Created by ukaodev on 2017/6/28.
 */

public class GatheringPesenter extends BasePresenter<GatheringView> {

    public GatheringPesenter(GatheringView view) {
        attachView(view);
    }

    /* 充值支付开关*/
    public void getPaymentConfig() {
        mvpView.showLoading();
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        addSubscription(apiStores.getPaymentConfig(Constant.createParameter(mHashMap)),
                new ApiCallback<PayInfoBean>() {
                    @Override
                    public void onSuccess(PayInfoBean model) {
                        if (model.getHttpCode() == 200) {

                            mvpView.rechargeShowswitch(model.getData());

                        } else {

                            mvpView.loadFail(model.getMsg());
                        }

                    }


                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }

    /*用户余额*/
    public void userExtbalance(String userId,final  boolean  isfirstload) {
        mvpView.showLoading();
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("id", userId);
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        addSubscription(apiStores.userExtbalance(Constant.createParameter(mHashMap)),
                new ApiCallback<BalanceBean>() {
                    @Override
                    public void onSuccess(BalanceBean model) {
                        if (model.getHttpCode() == 200) {
                            mvpView.useBalanceData(model.getData(),isfirstload);
                        } else {
                            mvpView.loadFail(model.getMsg());
                        }
                    }


                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }

    /**
     * 使查询订单优惠券
     *
     * @param orderId
     */
    public void couponList(String orderId) {
        mvpView.showLoading();
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("orderId", orderId);
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        addSubscription(apiStores.couponList(Constant.createParameter(mHashMap)),
                new ApiCallback<Coupons>() {
                    @Override
                    public void onSuccess(Coupons model) {
                        if (model.getHttpCode() == 200) {

                            mvpView.loadCouponList(model.getData());

                        } else {

                            mvpView.loadFail(model.getMsg());
                        }

                    }


                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }

    /**
     * 使用优惠券
     *
     * @param orderId
     */
    public void userCouponInfo(String orderId,final Coupons  mCoupons) {
        mvpView.showLoading();
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("orderId", orderId);
        mHashMap.put("relId", mCoupons.getId());/*订单优惠券id*/
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        addSubscription(apiStores.userCouponInfo(Constant.createParameter(mHashMap)),
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {
                        if (model.getHttpCode() == 200) {

                             mvpView.userCouponSucceed(mCoupons);

                        } else {

                              mvpView.loadFail(model.getMsg());
                        }

                    }


                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }


    /**
     *   优付卡支付
     * @param orderId  订单ID
     * @param balance   使用余额
     * @param isUseUPay  是否使用优付卡
     * @param cpnRelId   优惠卷  id
     */
    public void confirmPay(String orderId,final  int  balance,boolean isUseUPay,String  cpnRelId ) {
        mvpView.showLoading();

        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("orderId", orderId);
        if (!CheckUtils.isEmpty(cpnRelId)){
            mHashMap.put("cpnRelId", cpnRelId);
        }
        if(isUseUPay){
            mHashMap.put("groupCardType","41");//groupCardType 支付(团)卡类型 41-优付卡
        }else{
            mHashMap.put("balance", balance+"");
        }
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        L.i("confirmPay="+mHashMap.toString());
        addSubscription(apiStores.confirmPay(Constant.createParameter(mHashMap)),
                new ApiCallback<BalanceBean>() {
                    @Override
                    public void onSuccess(BalanceBean model) {
                        if (model.getHttpCode() == 200) {
                            PayEvent.postHasData(PayEvent.Message.PAY_SUCCEED,balance);
                        } else {
                            T.show(model.getMsg());
                        }

                    }


                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }


    /**
     * 余额支付
     * @param orderId
     * @param cpnRelId
     */
    public void balancePay(String orderId,String  cpnRelId ) {
        mvpView.showLoading();

        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("orderId",orderId);
        if (!CheckUtils.isEmpty(cpnRelId)){
            mHashMap.put("cpnRelId", cpnRelId);
        }
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        L.i("balancePay="+mHashMap.toString());
        addSubscription(apiStores.balancePay(Constant.createParameter(mHashMap)),
                new ApiCallback<BalanceBean>() {
                    @Override
                    public void onSuccess(BalanceBean model) {
                        if (model.getHttpCode() == 200) {
                            PayEvent.postHasData(PayEvent.Message.PAY_SUCCEED,balance);
                        } else {
                            T.show(model.getMsg());
                        }

                    }


                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }


    /**
     *付款订单查询  查询订单信息
     */
    public void gathering(final String  orderid  ) {
        mvpView.showLoading();
        HashMap     mHashMap=new HashMap<String,String>();
        mHashMap.put("id",orderid);
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        addSubscription(apiStores.gathering(Constant.createParameter(mHashMap)),
                new ApiCallback<PayInfoBean>() {
                    @Override
                    public void onSuccess(PayInfoBean model) {
                        if (model.getHttpCode()==200){
                            PayInfoBean  mPayInfoBean=model.getData();
                            mPayInfoBean.setOrderId(orderid);
                            mvpView.payInfoData(mPayInfoBean);
                        }else{
                            mvpView.loadFail(model.getMsg());
                        }


                    }


                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }

    /**
     *打印订单
     */
    public void printReceipt(Context context, String  id ) {
        mvpView.showLoading();
        HashMap mHashMap=new HashMap<String,String>();
        mHashMap.put("orderId",id);
        mHashMap.put("accessToken", SaveParamets.getToken(context));
        addSubscription(apiStores.printReceipt(Constant.createParameter(mHashMap)),
                new ApiCallback<ReceiptBean>() {
                    @Override
                    public void onSuccess(ReceiptBean model) {
                        if (model.getHttpCode()==200){
                            mvpView.loadReceipt(model.getData());
                        }else{
                            mvpView.loadFail(model.getMsg());
                        }


                    }



                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }


}
